<template>
	<el-form ref="detailForm" :model="detailForm" label-width="110px" class="detail-info">
	  	<el-form-item label="父级工艺分类" v-show="!hasParent">
	    	<el-input v-model="detailForm.PCode" :disabled="true"></el-input>
	  	</el-form-item>
	  	<el-form-item label="工艺分类Code">
	    	<el-input v-model="detailForm.Code" v-if="hasNew"></el-input>
	    	<el-input v-model="detailForm.Code" v-else :disabled="true"></el-input>
	  	</el-form-item>
	  	<el-form-item label="工艺分类名称">
	    	<el-input v-model="detailForm.Value"></el-input>
	  	</el-form-item>
	  	<el-form-item label="详细">
            <div class="edit_container">
				<editor ref="myTextEditor" :fileName="'file'" :uploadUrl="uploadUrl" v-model="detailForm.Remark"></editor>
            </div>
        </el-form-item>
        <el-form-item>
        	<el-button type="primary" @click="fnAddManufacture" v-show="hasNew">确认新增</el-button>
            <el-button type="primary" @click="fnUpdateManufacture" v-show="!hasNew">确认更新</el-button>
        	<el-button @click.native="fnBack">返回</el-button>
        </el-form-item>
	</el-form>
</template>

<script>
    import Svc from '../../service/index.js';
	import editor from '../../components/quillEditor/Quilleditor.vue'
	
	export default {
		data() {
			return {
				alertMsg:'',
				hasAlert:false,
				hasParent:true, //判断是新建父级工艺还是新建子级工艺
				hasNew:true,//判断当前是否为新建，还是更新
				currentID:0,//当前工艺ID
				//用户信息数据
				detailForm: {
					PCode:'',
					Code: '',
					Value:'',
					Remark: ''
				},
				//富文本编辑器->上传图片url
      			uploadUrl:'http://doc.cgb100.com/upload?cat=newsimg'
			}
		},
		methods: {
            //显示message提示框
            openMessage : function(msg,type){
    	        this.$message({
		          	showClose: true,
		          	message: msg,
		          	type: type
		        });
            },
            //获取当前工艺
            fnGetCurrentmanuf : function(id){
            	var that = this
	            Svc.fnGetData('/api/manuf/detail/'+id,{},function(res){
	            	if (res.body.ErrCode == 0){
	            		that.detailForm = res.body.Data
	            		that.detailForm.PCode = res.body.Data.ParentCode
	            		if (!res.body.Data.ParentCode) {
	            			that.hasParent = true;
	            		} else {
	            			that.hasParent = false;
	            		}
	            	} else {
	            		that.alertMsg = "制造工艺详情获取失败！"
	            		that.hasAlert = true
	            	}
	            },function(res){
	            	that.alertMsg = "网络错误，请重新！"
	            	that.hasAlert = true
	            }) 
            },
            //新增工艺
            fnAddManufacture : function(){
            	var that = this
	            Svc.fnPostData('/api/manuf/add',that.detailForm,function(res){
	            	if (res.body.ErrCode == 0){
	            		that.openMessage('制造工艺新增成功！',"success")
	            		that.$router.push({path: '/manufacture'})
	            	} else {
	            		that.openMessage(res.body.ErrMsg,"error")
	            	}
	            },function(res){
	            	that.alertMsg = "网络错误，请重新！"
	            	that.hasAlert = true
	            }) 
            },
            //更新工艺
            fnUpdateManufacture : function(){
            	var that = this
            	var params = {
            		ID:that.currentID,
					Value:that.detailForm.Value,
					Remark:that.detailForm.Remark
            	}
	            Svc.fnPostData('/api/manuf/update',params,function(res){
	            	if (res.body.ErrCode == 0){
	            		that.openMessage('制造工艺更新成功！',"success")
	            		that.$router.push({path: '/manufacture'})
	            	} else {
	            		that.openMessage(res.body.ErrMsg,"error")
	            	}
	            },function(res){
	            	that.alertMsg = "网络错误，请重新！"
	            	that.hasAlert = true
	            }) 
            },
            //返回
            fnBack : function(){
              	this.$router.push({ path: '/manufacture' });
            }
		},
	    components: {
		    editor
		},
		mounted() {
			var that = this;
			var opttype = that.$route.query.opttype;
			if (opttype == 'newParent') {
				that.hasParent = true;
				that.hasNew = true;
			} else if (opttype == 'newChild') {
				that.hasParent = false;
				that.hasNew = true;
				that.detailForm.PCode = that.$route.query.pCode;
			} else if (opttype == 'update') {
				that.hasNew = false;
				that.currentID = that.$route.query.currentID;
				that.fnGetCurrentmanuf(that.currentID)
			}
		}
	}

</script>

<style scoped>
	.detail-info{
		margin:20px;
		width:90%;
		min-width:600px;
	}
	
	/*设置富文本编辑器高度*/
	.edit_container{
		height: 435px;
	}
</style>